﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NFine.Application.Biz;
using NFine.Code;

namespace NFine.Web.Areas.BizManage.Controllers
{
    public class BalanceRecordController : ControllerBase
    {
        private BalanceRecordApp balanceRecordApp = new BalanceRecordApp();
        private MemberBonusApp memberBonusApp = new MemberBonusApp();
        private UserDetailApp userDetailApp = new UserDetailApp();
        private UserOrderApp userOrderApp = new UserOrderApp();

        //
        // GET: /BizManage/BalanceRecord/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult BalanceAdd()
        {

            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        [HandlerLogin]
        public ActionResult GetListForBalance(Pagination pagination,  string YearRecord)
        {
            //userDetailApp.BalanceForMonth();
            var data = new
            {
                rows = balanceRecordApp.GetListForBalance(pagination, YearRecord),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult BalanceDetail(Pagination pagination)
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonByBalanceId(Pagination pagination, string balanceId)
        {
            var data = new
            {
                rows = memberBonusApp.GetListByBalanceId(pagination, balanceId),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(string txtDate)
        {
            DateTime date = Convert.ToDateTime(txtDate);

            //判断是否已经结算，或者结算金额为0
            decimal dResult = userOrderApp.SumAllOrderByMonth(date);
            if(dResult <= 0)
                return Error(txtDate + "，已经结算或者结算金额为0！");

            int iResult = userDetailApp.BalanceForMonthNew(date);

            //bool bResult = userDetailApp.BalanceForMonth(date);
            if (iResult > 0)
            {
                return Success(txtDate+"，结算成功！");
            }
            else
                return Error(txtDate + "，结算失败！");


        }

    }
}
